package string;

/**
 * @author LIBOFEI
 * @ClassName SplitString
 * @description
 * @date 2020/10/30 09:52
 * @Version 1.0
 **/
public class SplitString {

    public static void main(String[] args) {
        String password = "abc1";
        String password3 = "123";
        String password1 = "abc123";
        String password2 = "12ab3d";
        if (isContinuousChar(password)) {
            System.out.println("password 包含3个及以上相同或字典连续字符");
        }
        if (isContinuousChar(password3)) {
            System.out.println("password3 包含3个及以上相同或字典连续字符");
        }
        if (isContinuousChar(password1)) {
            System.out.println("password1 包含3个及以上相同或字典连续字符");
        }
        if (isContinuousChar(password2)) {
            System.out.println("password2 包含3个及以上相同或字典连续字符");
        }
    }

    /**
     * 是否包含3个及以上相同或字典连续字符
     */
    private static boolean isContinuousChar(String password) {
        char[] chars = password.toCharArray();
        for (int i = 0; i < chars.length - 2; i++) {
            int n1 = chars[i];
            int n2 = chars[i + 1];
            int n3 = chars[i + 2];
            // 判断重复字符
            if (n1 == n2 && n1 == n3) {
                return true;
            }
            // 判断连续字符： 正序 + 倒序
            if ((n1 + 1 == n2 && n1 + 2 == n3) || (n1 - 1 == n2 && n1 - 2 == n3)) {
                return true;
            }
        }
        return false;
    }
}
